package com.nova.android.tvlauncher.ui.view

import android.R.id.tabs
import android.content.Context
import android.util.AttributeSet
import android.view.KeyEvent
import android.view.LayoutInflater
import android.widget.FrameLayout
import android.widget.LinearLayout
import androidx.databinding.DataBindingUtil.getBinding
import com.nova.android.tvlauncher.AppCache
import com.nova.android.tvlauncher.JarLoaderBackup.onBind
import com.nova.android.tvlauncher.R
import com.nova.android.tvlauncher.databinding.TabItemNavigationBinding
import com.nova.android.tvlauncher.databinding.ViewMainTopAtvBinding
import com.nova.android.tvlauncher.ui.adapter.atv.HomeTabAdapter
import com_github_lib_base.ext.dimenValue
import com_github_lib_base.ext.rv.setup
import com_github_lib_base.ext.stringValue

class MainTopAtvView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

    private var binding: ViewMainTopAtvBinding =
        ViewMainTopAtvBinding.inflate(LayoutInflater.from(context), this, true)

    init {
        HomeTabAdapter.selectedPosition = 0
        HomeTabAdapter.lastFocusedPosition = -1
        setupListeners()
    }

    fun setVoiceFocus(isFocus:Boolean){
        binding.viewMainTopLeftAtv.setVoiceFocus(isFocus)
    }

    fun mainTopBinding() = binding

    private fun setupListeners() {
        val tabs = arrayListOf(com.nova.lib_lang.R.string.main_txt_4.stringValue(), com.nova.lib_lang.R.string.main_txt_5.stringValue())

        binding.tabLayout.setup(tabs) {
            addType(String::class, R.layout.tab_item_navigation)
            onBind {

                val itemBinding = getBinding<TabItemNavigationBinding>()
                val item = getModel<String>()

                val isShowVoice = AppCache.configLocalDto?.isShowVoiceIcon != false
                if(!isShowVoice)itemBinding.root.nextFocusLeftId = R.id.tv_keyboard

                HomeTabAdapter.bindData(itemBinding,modelPosition,models,itemView,binding.tabLayout)

                itemView.setOnKeyListener { _, keyCode, event ->
                    if (keyCode == KeyEvent.KEYCODE_DPAD_UP) return@setOnKeyListener true
                    return@setOnKeyListener false
                }

            }
        }

        binding.tabLayout.setNumRows(1)
        binding.tabLayout.setItemSpacing(com.codeZeng.lib_dimen.R.dimen.qb_px_16.dimenValue())


        binding.tabLayout.setOnChildSelectedListener { _, view, position, _ ->
            if (lastSelectedPosition == position) return@setOnChildSelectedListener // 防止重复调用
            val tabMenu = view?.findViewById<LinearLayout>(R.id.tab_menu_item_container)
            if(position==0){
                tabMenu?.nextFocusDownId = R.id.tv_home_banner_title
            }else{
                tabMenu?.nextFocusDownId = R.id.tv_apps_banner_title
            }
            lastSelectedPosition = position
            onTabSelectedListener?.invoke(position)
        }
    }
    private var lastSelectedPosition: Int = -1

    private var onTabSelectedListener: ((position: Int) -> Unit)? = null
    fun onTabSelected(listener: (position: Int) -> Unit) {
        onTabSelectedListener = listener
    }

    fun setupWithViewPager() {

    }

}